﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CommonHelper
{
    /// <summary>
    /// 相对于ApiResult类，其中的Data属性可以是任意类型，而不仅仅是object类型
    /// 但是返回的时候swagger不知道其具体返回的是什么类型的参数，所以还是考虑使用ApiResult类。
    /// </summary>
    public class ApiResponse
    {
        /// <summary>
        /// 响应码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 响应信息 
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 具体数据
        /// </summary>
        public dynamic Data { get; set; }

        /// <summary>
        /// 成功状态返回
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static ApiResponse SuccessResult(dynamic Data)
        {
            return new ApiResponse()
            {
                Code = 0,
                Message = "success",
                Data = Data
            };
        }

        /// <summary>
        /// 失败状态返回
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static ApiResponse FailResult(string msg)
        {
            return new ApiResponse()
            {
                Code = 1,
                Message = msg
            };
        }

        /// <summary>
        /// 异常状态返回
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static ApiResponse ErrorResult(Exception ex)
        {
            return new ApiResponse()
            {
                Code = 1,
                Message = ex.Message
            };
        }

        /// <summary>
        /// 自定义状态返回
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ApiResponse MyResult(int code, string message, dynamic data)
        {
            return new ApiResponse()
            {
                Code = code,
                Message = message,
                Data = data
            };
        }
    }
}
